/*
 * Copyright 2012 Benoît Combe <benoit.combe@gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.ipst.spot.formulaires.entites;

import fr.ipst.spot.entites.Document;
import fr.ipst.spot.entites.Evenement;
import fr.ipst.spot.exceptions.SpotExceptionForm;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @param <T>
 * @author Benoît Combe <benoit.combe@gmail.com>
 */
public abstract class FormAbstractDocument<T extends Document> extends FormAbstractHtmlEntite<T> {

    private String titre;
    private String resume;
    private Date date;
    private FormEvenementId idEvenement;

    /**
     *
     * @param formId
     * @param entity
     */
    public FormAbstractDocument(FormAbstractHtmlEntiteId<T> formId, T entity) {
        super(formId, entity);
        this.titre = entity.getTitre();
        this.resume = entity.getResume();
        this.date = entity.getDate();
    }

    /**
     *
     * @param formId
     * @param request
     */
    public FormAbstractDocument(FormAbstractHtmlEntiteId<T> formId, HttpServletRequest request) {
        super(formId, request);
        this.resume = this.getNonNullStringFromRequest(request, "resume");
        this.titre = this.getNonNullStringFromRequest(request, "titre");
        this.date = this.getDateFromRequest(request, "date", "dd/MM/yyyy");
        this.idEvenement = new FormEvenementId(request);
    }

    @Override
    protected T garnir(T entityToFeed, boolean creation) throws SpotExceptionForm {
        if (creation) {
            entityToFeed.getEvenementList().add(new Evenement(idEvenement.getId()));
        }
        entityToFeed.setDate(date);
        entityToFeed.setResume(resume);
        entityToFeed.setTitre(titre);
        return entityToFeed;
    }

    @Override
    protected void validate(boolean creation) throws SpotExceptionForm {
        boolean erreur = false;
        if (creation) {
            try {
                idEvenement.validate();
            } catch (SpotExceptionForm e) {
                erreur = true;
                this.concatErreurMsg(e);
            }
        } else {
            try {
                this.formId.validate();
            } catch (SpotExceptionForm e) {
                erreur = true;
                this.concatErreurMsg(e);
            }
        }
        try {
            this.isTitreDocument(titre);
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        if (!resume.isEmpty()) {
            try {
                this.isLongText(resume, "résumé");
            } catch (SpotExceptionForm e) {
                this.concatErreurMsg(e);
            }
        }
        if (date == null) {
            erreur = true;
            this.concatErreurMsg(new SpotExceptionForm("La date est obligatoire"));
        }
        if (erreur) {
            throw new SpotExceptionForm(msgErreurValidate);
        }
    }

    /**
     *
     * @return
     */
    public Date getDate() {
        return date;
    }

    /**
     *
     * @param date
     */
    public void setDate(Date date) {
        this.date = date;
    }

    /**
     *
     * @return
     */
    public String getResume() {
        return resume;
    }

    /**
     *
     * @param resume
     */
    public void setResume(String resume) {
        this.resume = resume;
    }

    /**
     *
     * @return
     */
    public String getTitre() {
        return titre;
    }

    /**
     *
     * @param titre
     */
    public void setTitre(String titre) {
        this.titre = titre;
    }

    /**
     *
     * @return
     */
    public Integer getIdEvenement() {
        return idEvenement.getId();
    }

    /**
     *
     * @param idEvenement
     */
    public void setIdEvenement(Integer idEvenement) {
        this.idEvenement.setId(idEvenement);
    }
}